package com.situ.mall.admin.controller;

import com.situ.mall.admin.entity.User;
import com.situ.mall.common.entity.Onetoone;
import com.situ.mall.common.mapper.OnetooneMapper;
import com.situ.mall.common.service.IOnetooneService;
import com.situ.mall.common.util.RequestUtil;
import com.situ.mall.common.vo.TableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author yangkx
 * @create 2024-04-24-15:09
 */
@Controller
@RequestMapping("/admin")
public class ServiceController {

    @Autowired
    private IOnetooneService onetooneService;

    @Autowired
    private OnetooneMapper onetooneMapper;

    // 打开服务管理页面
    @GetMapping("/service/page")
    public String page() {
        return "service";
    }

    // 查询一对一服务列表
    @GetMapping("/service/oneToOne")
    @ResponseBody
    public TableVO table(Integer page, Integer limit, String serviceName) {
        return onetooneService.table(page, limit, serviceName);
    }

    // 更新状态
    @PostMapping("/service/oneToOneStatus")
    @ResponseBody
    public void status(Integer id,String status){
        Onetoone onetoone = new Onetoone();
        onetoone.setId(id);
        onetoone.setStatus(status);
        //有值的更新，没有值的不更新
        onetooneService.updateById(onetoone);
    }

    //保存
    @PostMapping("/service/saveOne")
    @ResponseBody
    public void saveOne(Onetoone onetoone){
        //判断是新增还是编辑
        if (onetoone.getId() == null) {
            onetoone.setCreateUserid(((User) RequestUtil.getCurrent()).getId()); // 发布人id（当前登录用户id）
        }
        onetooneService.saveOne(onetoone);
    }

    //编辑——根据ID查询该行数据带到前台
    @GetMapping("/service/getOne/{id}")
    @ResponseBody
    public Onetoone getOne(@PathVariable Integer id) {
        return onetooneService.getById(id);
    }
}
